package cn.tedu.eguser1.controller;

import cn.tedu.eguser1.base.response.JsonResult;
import cn.tedu.eguser1.base.response.StatusCode;
import cn.tedu.eguser1.mapper.UserMapper;
import cn.tedu.eguser1.pojo.dto.UserAddParam;
import cn.tedu.eguser1.pojo.dto.UserUpdateParam;
import cn.tedu.eguser1.pojo.entity.User;
import cn.tedu.eguser1.pojo.vo.UserListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Validated
@RestController
@Tag(name = "用户管理模块")
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    /**
     * 1.添加用户
     */
    @PostMapping("add")
    @Operation(summary = "添加用户")
    @ApiOperationSupport(order = 10)
    public JsonResult add(@RequestBody @Validated UserAddParam userAddParam){
        System.out.println("userAddParam = " + userAddParam);
        User user = new User();
        BeanUtils.copyProperties(userAddParam, user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        System.out.println("user = " + user);
        int i = userMapper.insertUser(user);
        if (i > 0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }

    /**
     * 2.查询用户列表功能
     */
    @GetMapping("list")
    @Operation(summary = "用户列表")
    @ApiOperationSupport(order = 20)
    public JsonResult list(){
        List<UserListVO> userListVOS = userMapper.list();
        System.out.println("userListVOS = " + userListVOS);

        return JsonResult.ok(userListVOS);
    }

    /**
     * 3.删除用户功能
     */
    @PostMapping("del")
    @Operation(summary = "删除用户")
    @ApiOperationSupport(order = 30)
    @Parameter(name = "id", description = "用户编号", required = true)
    public JsonResult del(@Range(min = 1, max = 100, message = "用户编号必须在1-100之间") Long id){
        System.out.println("id = " + id);
        int i = userMapper.deleteUserById(id);
        if (i > 0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }

    /**
     * 4.修改用户功能
     */
    @PostMapping("update")
    @Operation(summary = "修改用户")
    @ApiOperationSupport(order = 40)
    public JsonResult update(@RequestBody UserUpdateParam userUpdateParam){
        System.out.println("userUpdateParam = " + userUpdateParam);
        User user = new User();
        BeanUtils.copyProperties(userUpdateParam, user);
        user.setUpdateTime(new Date());
        System.out.println("user = " + user);
        int i = userMapper.updateUser(user);
        if (i > 0){
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }
}











